module.exports = function ($scope,$stateParams,RequestService,$localStorage,modelService,commonConfig,FileUploader,$modal,$modalInstance,data) {
    $scope.User = $localStorage.get('cloud');
    console.log($stateParams.id,data);
    if(data){
        $scope.form = {
            "appCommonlyFunctionIcon": data.appCommonlyFunctionIcon,
            "appCommonlyFunctionName": data.appCommonlyFunctionName,
            "appCommonlyFunctionNo": data.appCommonlyFunctionNo,
            "appCommonlyFunctionUrl": data.appCommonlyFunctionUrl,
            "appId": data.appId,
            "id": data.id
        };
    }else {
        //数据初始化
        $scope.form = {
            "appCommonlyFunctionIcon": null,
            "appCommonlyFunctionName": null,
            "appCommonlyFunctionNo": null,
            "appCommonlyFunctionUrl": null,
            "appId": $stateParams.id,
            "id": 0
        };
    }
    //图片上传设置
    var uploader = $scope.uploader = new FileUploader({
        url: commonConfig.$webroot+commonConfig.dfsApi+'file?access_token='+$scope.User.token,  //固定的地址
        queueLimit: 1 //文件个数限制
        //removeAfterUpload: true //上传后删除文件
    });
    // FILTERS
    uploader.filters.push({
        name: 'imageFilter',
        fn: function (item /*{File|FileLikeObject}*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
    });
    uploader.onWhenAddingFileFailed = function (item /*{File|FileLikeObject}*/, filter, options) {
        console.info('添加文件到上传队列失败后', item, filter, options);
    };
    uploader.onAfterAddingAll = function (addedFileItems) {
        $scope.uploader.uploadAll();
        console.info('添加所选的所有文件到上传队列后', addedFileItems);
    };
    uploader.onCompleteItem = function (fileItem, response, status, headers) {
        console.info('文件上传完成后', fileItem, response, status, headers);
        $scope.form.appCommonlyFunctionIcon = response.uuid;
    };


    $scope.ok = function () {  //提交应用
        RequestService.Request(commonConfig.cloudApi+'cloud/appcommonlyfunction/' + (data ? data.id: ''), data ? 'PUT' : 'POST', $scope.form).then(function (res) {
            console.log(res);
            if(res.code == 0){
                $modalInstance.close();
            }else {
                modelService.openTipsModel('提交失败！');
            }
        }, function (error) {
            console.log(error);
        });
    };

    //关闭模态框
    $scope.cancel = function () {
        $modalInstance.dismiss();
    }
};